import TestPaperEntity from "../entities/TestPaperEntity";
import TestPaperModel from "../models/TestPaperModel";

export default class TestPaperDao {

  static async create(paper: TestPaperEntity) {
    const result = await TestPaperModel.create({
      ...paper,
      questions: JSON.stringify(paper.questions)
    })

    return result.toJSON()
  }

  static async findByPage(offset: number = 0, limit: number = 10) {
    // TODO 连表查询，根据题目id 查题目相关信息
    const { rows, count } = await TestPaperModel.findAndCountAll({
      offset,
      limit
    })

    return {
      total: count,
      data: rows
    }
  }

  static async delete(id: string = '') {
    const result = await TestPaperModel.destroy({
      where: {
        id
      }
    })
    return result > 0 ? true : false
  }

  static async findById(id: string = "") {
    const result = await TestPaperModel.findByPk(id)
    return result?.toJSON()
  }

}